package com.nintendo.frame.edit;

import com.nintendo.entity.pojo.Customer;
import com.nintendo.frame.CustomerFrame;
import com.nintendo.mapper.CustomerDao;
import com.nintendo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditCustomerFrame extends JFrame {

    private JTextField nameField;
    private JTextField sexField;
    private JTextField companyField;
    private JTextField telField;
    private JTextField cardidField;
    private CustomerFrame parentFrame;
    private Customer customer;

    public EditCustomerFrame(CustomerFrame parentFrame, Customer customer) {
        this.parentFrame = parentFrame;
        this.customer = customer;
        setTitle("修改客户");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建面板
        JPanel panel = new JPanel(new GridLayout(6, 2));
        panel.add(new JLabel("姓名:"));
        nameField = new JTextField(customer.getName());
        panel.add(nameField);

        panel.add(new JLabel("性别:"));
        sexField = new JTextField(customer.getSex());
        panel.add(sexField);

        panel.add(new JLabel("公司:"));
        companyField = new JTextField(customer.getCompany());
        panel.add(companyField);

        panel.add(new JLabel("电话:"));
        telField = new JTextField(customer.getTel());
        panel.add(telField);

        panel.add(new JLabel("身份证号:"));
        cardidField = new JTextField(customer.getCardid());
        panel.add(cardidField);

        JButton editButton = new JButton("修改");
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editCustomer();
            }
        });

        panel.add(editButton);

        setContentPane(panel);
    }

    private void editCustomer() {
        String name = nameField.getText();
        String sex = sexField.getText();
        String company = companyField.getText();
        String tel = telField.getText();
        String cardid = cardidField.getText();

        if (name.isEmpty() || sex.isEmpty() || company.isEmpty() || tel.isEmpty() || cardid.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有字段", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        customer.setName(name);
        customer.setSex(sex);
        customer.setCompany(company);
        customer.setTel(tel);
        customer.setCardid(cardid);

        try (SqlSession session = MyBatisUtils.getSqlSession()) {
            CustomerDao customerDao = session.getMapper(CustomerDao.class);
            customerDao.update(customer);
            session.commit();
            JOptionPane.showMessageDialog(this, "修改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            dispose();
            parentFrame.refreshTable();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "修改失败", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new EditCustomerFrame(null, new Customer()).setVisible(true);
        });
    }
}
